package com.jsb.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.jsb.service.GainmedalService;
import com.jsb.service.UserService;
import com.jsb.common.utils.StringUtils;
import com.jsb.model.Gainmedal;
import com.jsb.model.GainmedalExample;
import com.jsb.model.GainmedalExample.Criteria;
import com.jsb.model.User;

@Controller
public class GainmedalController{
	
	private int defaultPageSize=10;
	
	private String redirect = "redirect:/gainmedal/list.do";
	
	@Resource
	private GainmedalService gainmedalService;
	@Resource
	private UserService userService;
	
	@RequestMapping("/gainmedal/list")
	public String list(
			Model model,String finish,String phone,
			@RequestParam(value = "pageNum", required = false) Integer pageNum,
			@RequestParam(value = "pageSize", required = false) Integer pageSize) {
		int maxPageNum = 0;
		if (pageSize == null) {
			pageSize = defaultPageSize;
		}
		GainmedalExample gainmedalExample = new GainmedalExample();
		List<User> userByLikePhone = userService.getUserByLikePhone(phone);
		for (User user : userByLikePhone) {
			String userid = user.getPhoneNumber();
			Criteria createCriteria = gainmedalExample.createCriteria();
			if(StringUtils.isNotEmpty(userid)){
				createCriteria.andPhoneLike("%"+userid+"%");
			}
			if(StringUtils.isNotEmpty(finish)&&finish.equals("0")){
				createCriteria.andMedalNum1GreaterThan(0);
				createCriteria.andMedalNum2GreaterThan(0);
				createCriteria.andMedalNum3GreaterThan(0);
				createCriteria.andMedalNum4GreaterThan(0);
				createCriteria.andMedalNum5GreaterThan(0);
				createCriteria.andMedalNum6GreaterThan(0);
			}
			gainmedalExample.or(createCriteria);
		}
		int totalCount = gainmedalService.countGainmedal(gainmedalExample);
		if(totalCount%pageSize == 0){
			maxPageNum = totalCount/pageSize;
		}else{
			maxPageNum = totalCount/pageSize+1;
		}
		if (pageNum == null || pageNum < 1) {
			pageNum = 1;
		}else {
			if(maxPageNum > 0 && pageNum > maxPageNum) {
				pageNum = maxPageNum;
			}
		}
		gainmedalExample.setPageIndex((pageNum - 1) * pageSize);
		gainmedalExample.setPageSize(pageSize);
		List<Gainmedal> gainmedals = gainmedalService.getPageGainmedal(gainmedalExample);
		model.addAttribute("totalCount", totalCount);
		model.addAttribute("pageNum", pageNum);
		model.addAttribute("pageSize", pageSize);
		if(totalCount%pageSize == 0){
			model.addAttribute("maxPageNum", totalCount/pageSize);
		}else{
			model.addAttribute("maxPageNum", totalCount/pageSize+1);
		}
		model.addAttribute("gainmedals", gainmedals);
		model.addAttribute("phone", phone);
		model.addAttribute("finish", finish);
		return "/Gainmedal/list";
	}
	
	@RequestMapping("/gainmedal/toAdd")
	public String toAdd(Model model) {
		return "/Gainmedal/add";
	}

	@RequestMapping("/gainmedal/add")
	public String add(Model model, @ModelAttribute("gainmedal") Gainmedal gainmedal) {
		gainmedalService.addGainmedal(gainmedal);
		return redirect;
	}

	@RequestMapping("/gainmedal/del")
	public String del(Model model,
			@RequestParam(value = "phone", required = true) String phone) {
		gainmedalService.delGainmedalByPhone(phone);
		return redirect;
	}

	@RequestMapping("/gainmedal/toEdit")
	public String toEdit(Model model,
			@RequestParam(value = "phone", required = true) String phone) {
		Gainmedal gainmedal = gainmedalService.getGainmedalByPhone(phone);
		model.addAttribute("gainmedal", gainmedal);
		return "/Gainmedal/edit";
	}

	@RequestMapping("/gainmedal/edit")
	public String edit(Model model, @ModelAttribute("gainmedal") Gainmedal gainmedal) {
		gainmedalService.updateGainmedal(gainmedal);
		return redirect;
	}

	@RequestMapping("/gainmedal/view")
	public String view(Model model,
			@RequestParam(value = "phone", required = true) String phone) {
		Gainmedal gainmedal = gainmedalService.getGainmedalByPhone(phone);
		model.addAttribute("gainmedal", gainmedal);
		return "/Gainmedal/view";
	}
}
